{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a772b242",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cc65e62",
   "metadata": {},
   "outputs": [],
   "source": [
    "#first we need to learn about selenium to understrand to scrape shopping sites \n",
    "!pip install selenium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8ef089f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Selenium renders the JS and the fetches the content from it \n",
    "# It uses pip install webdriver-manager to make he python code to UI management possible\n",
    "\n",
    "!pip install webdriver-manager"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4854ae29",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from webdriver_manager.chrome import ChromeDriverManager\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "import time\n",
    "service = Service(ChromeDriverManager().install())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a00bc3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def scrapper_amazon(product_name):\n",
    "  driver = webdriver.Chrome(service=service)\n",
    "  driver.get(\"https://www.amazon.in/\")\n",
    "  try:\n",
    "    search_bar_locator = (By.ID,\"twotabsearchtextbox\")\n",
    "    search_button_locator = (By.ID, \"nav-search-submit-button\")\n",
    "\n",
    "    search_bar = WebDriverWait(driver,10).until(EC.presence_of_element_located(search_bar_locator))\n",
    "\n",
    "    search_bar.send_keys(product_name)\n",
    "    driver.find_element(*search_button_locator).click()\n",
    "    print(f\"{product_name}\")\n",
    "    # Get the first product card element\n",
    "    first_product = driver.find_elements(By.XPATH, \"//div[@class='puisg-row']\")\n",
    "    first_product = [e for e in first_product if e]\n",
    "    top5 = first_product[:5]\n",
    "    top_5_text = [e.text for e in top5]\n",
    "    driver.quit()\n",
    "    return top_5_text\n",
    "\n",
    "  except Exception as e:\n",
    "    print(e)\n",
    "    driver.quit()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09eb6987",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def scrapper_flipkart(product_name):\n",
    "    driver = webdriver.Chrome(service=service)\n",
    "    driver.get(\"https://www.flipkart.com/\")\n",
    "\n",
    "    try:\n",
    "        try:\n",
    "            close_btn = WebDriverWait(driver, 5).until(\n",
    "                EC.element_to_be_clickable((By.XPATH, \"//span[text()='✕']\"))\n",
    "            )\n",
    "            close_btn.click()\n",
    "        except:\n",
    "            pass  # popup didn't appear\n",
    "\n",
    "        # --- 2. Find search box ---\n",
    "        search_bar_locator = (By.NAME, \"q\")\n",
    "        search_bar = WebDriverWait(driver, 10).until(\n",
    "            EC.presence_of_element_located(search_bar_locator)\n",
    "        )\n",
    "        search_bar.send_keys(product_name)\n",
    "        search_bar.submit()\n",
    "\n",
    "        print(f\"Searching Flipkart for: {product_name}\")\n",
    "        time.sleep(3)\n",
    "\n",
    "       \n",
    "        product_cards = driver.find_elements(By.XPATH, \"//div[contains(@class, 'yKfJKb')]\")\n",
    "\n",
    "        top5 = product_cards[:5]\n",
    "        \n",
    "        top5_text = [card.text for card in top5]\n",
    "        driver.quit()\n",
    "        return top5_text\n",
    "\n",
    "    except Exception as e:\n",
    "        print(\"Error:\", e)\n",
    "        driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a42e65c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def scrape_sites(search_product):\n",
    "  amaL = scrapper_amazon(search_product)\n",
    "  FlipL = scrapper_flipkart(search_product)\n",
    "  \n",
    "  return {\"amazonR\":amaL,\"flipkartR\":FlipL}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bea9cd31",
   "metadata": {},
   "outputs": [],
   "source": [
    "def content_call(product_name):\n",
    "  results = scrape_sites(product_name)\n",
    "  amazonR = results[\"amazonR\"]\n",
    "  flipkartR = results[\"flipkartR\"]\n",
    "  print(amazonR)\n",
    "  amazon_newline = \"\\n\".join(amazonR)\n",
    "  print(amazon_newline)\n",
    "  flipkart_newline = \"\\n\".join(flipkartR)\n",
    "  userContent = f\"\"\"\n",
    "  Here are the options of amazon for the {product_name} search : \n",
    "  {amazon_newline}\n",
    "\n",
    "  Flipkart:\n",
    "  {flipkart_newline}\n",
    "\n",
    "  Compare them both and let me know who offer better offer \n",
    "  \n",
    "  make sure to take the discounted price while comparing leave out the MRP\n",
    "  \"\"\"\n",
    "  return userContent\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc9c6da2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def messages_sysNuser(product_name):\n",
    "  system_prompt =\"\"\"\n",
    "  You are a product comparison AI.\n",
    "\n",
    "Your job:\n",
    "- Compare product prices from Amazon and Flipkart.\n",
    "- Use ONLY the discounted price (ignore MRP).\n",
    "- Extract a clean comparison table.\n",
    "- Output MUST follow this structure:\n",
    "\n",
    "### 🛒 Product Comparison Summary\n",
    "### 📦 Amazon Listings\n",
    "### 📦 Flipkart Listings\n",
    "### 📊 Extracted Comparison Table\n",
    "### 🏆 Final Recommendation\n",
    "\n",
    "Format EVERYTHING in clean Markdown.\n",
    "No disclaimers. No extra text.\n",
    "\n",
    "  \"\"\"\n",
    "  return [\n",
    "    {\"role\":\"system\",\"content\":system_prompt},\n",
    "    {\"role\":\"user\",\"content\":content_call(product_name)}\n",
    "  ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ba9d8fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import api_key\n",
    "\n",
    "\n",
    "def search_product(product_name):\n",
    "  openai = OpenAI(api_key=api_key)\n",
    "  response = openai.chat.completions.create(\n",
    "    model = \"gpt-4.1-nano\",\n",
    "    messages = messages_sysNuser(product_name)\n",
    "  )\n",
    "  return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec498c0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Markdown, display\n",
    "product_name =\"Boat Rockerz 650\"\n",
    "display(Markdown(search_product(product_name)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92a9e2eb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
